package com.sofwin.controller;

import com.github.pagehelper.PageInfo;
import com.sofwin.pojo.SysRole;
import com.sofwin.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * @program: ssm-parent
 * @description: 角色管理
 * @author: Mr.Gao
 * @create: 2021-04-17 17:24
 **/
@Controller
@RequestMapping("system/role")
public class RoleController {

    @Autowired
    private RoleService roleService;

    
    /** 
    * @Description: 角色查询，分页
    * @param role: 角色实体类
     * @param pageNumber: 当前页码
     * @param pageSize: 每页的条数
     * @param model: model作用域
    * @return: String
    * @Author: Mr.Gao
    * @Date: 2021/4/17
    */
    @RequestMapping("data")
    public String data(SysRole role, @RequestParam( required = true,defaultValue = "1") Integer pageNumber,
                       @RequestParam(required = true,defaultValue ="5") Integer pageSize, Model model){
        PageInfo<SysRole> page = roleService.selectRolesByPage(role,pageNumber,pageSize);
        model.addAttribute("data",page.getList());
        model.addAttribute("page",page);
        model.addAttribute("role",role);
        return "role/data";
    }


    /**
    * @Description: 批量删除
    * @param ids: 通过id删除
    * @return: Map
    * @Author: Mr.Gao
    * @Date: 2021/4/17
    */
    @RequestMapping(value = "{ids}",method = RequestMethod.DELETE)
    @ResponseBody
    public Map deletes( @PathVariable Integer[] ids){
        boolean status = roleService.deleteByID(ids);
        Map map = new HashMap();
        map.put("status",status);
        return map;
    }


    /**
    * @Description: 同于角色新增/编辑(接收数据)
    * @param model: model作用域
     * @param id: 获取角色id
    * @return: String
    * @Author: Mr.Gao
    * @Date: 2021/4/17
    */
    @RequestMapping(value = "{id}")
    public String toEdit( Model model,@PathVariable Integer id){
        //编辑
        if(id!=null && id!=0){
            SysRole role = roleService.selectById(id);
            model.addAttribute("role",role);
        }
        return "role/edit";
    }


    /**
    * @Description: 角色保存数据 新增/编辑(处理)
    * @param role: 角色实体类
    * @return: Map
    * @Author: Mr.Gao
    * @Date: 2021/4/17
    */
    @RequestMapping(value = "{w}",method = RequestMethod.POST)
    @ResponseBody
    public Map save(@PathVariable String w ,SysRole role){
        boolean status = roleService.saveRole(role);
        Map map = new HashMap();
        map.put("status",status);
        return map;
    }


    /**
    * @Description: 权限 资源树视图
    * @param roleId: 角色id
    * @param model: model作用域
    * @return: String
    * @Author: Mr.Gao
    * @Date: 2021/4/17
    */
    @RequestMapping("toPermission")
    public String toPermission(Integer roleId,Model model){
        model.addAttribute("roleId",roleId);
        return "role/permission";
    }

    /**
     * 保存权限，从数据库中增加或者删除一条数据（sys_role_resource）
     * @param roleId 角色Id
     * @param resourceId 资源ID
     * @param flag 状态（是否被选中）
     * @return
     */
    @RequestMapping("savePermission1")
    @ResponseBody
    public Map savePermission(Integer roleId,Integer resourceId,Integer flag){
        Map map = new HashMap();
        boolean status = roleService.savePermission(roleId,resourceId,flag);
        map.put("status",status);
        return map;
    }


    /**
    * @Description: 保存权限，从数据库中增加或者删除一条数据（sys_role_resource）
    * @param roleId: 角色Id
    * @param resourceIds: 资源ID
    * @return: Map
    * @Author: Mr.Gao
    * @Date: 2021/4/17
    */
    @RequestMapping(method = RequestMethod.POST)
    @ResponseBody
    public Map saveRolePermission(Integer roleId,@RequestParam(value = "resourceIds[]") Integer[] resourceIds){
        Map map = new HashMap();
        boolean status = roleService.savePermissions(roleId,resourceIds);
        map.put("status",status);
        return map;
    }


}
